using Godot;
using Godot.Collections; 
using System;

public partial class Splice : Interactable
{
  [Export]
  public string mapname = "";//对应的地图名，不是所在的地图
  [Export]
  public int id;//1-4对应四个方向
  private Map _map;
  private MapManager _mapManager;
  private Player _player;
    public override void _Ready()
    {
        base._Ready();
        var Game = GetNode<Game>("/root/Game");
      _mapManager = Game.get_main().GetNode<MapManager>("%MapManager");
      _player = Game.get_player();
    }

    public override void Interact()
    {
      var handmapname = (_player.CharacterData as PlayerData).MapinHand;
      if(mapname != "")
      {
        //关闭地图，检测函数
        GD.Print("关闭地图名",mapname);
        Dismap();
        return;
      }
      CollisionShape2D area =this.GetChild(0) as CollisionShape2D;
      area.Disabled = true;
      area.Disabled = false;
      if(handmapname == null||handmapname == ""||handmapname == "随机")
      {
        RandAddMap();
        return;
        }
      AddMap();
    
      
    }

    public void AddMap()
    {
      var handmapname = (_player.CharacterData as PlayerData).MapinHand;
      var pos = GetNewMapPosition();
      //GD.Print(handmapname);
      if(_mapManager.AddMap(handmapname ,pos,this.id,Owner.Name.ToString().ToLower()))
      {
        mapname = handmapname;
        (_player.CharacterData as PlayerData).MapinHand = "";
        (_player.CharacterData as PlayerData).Maps.Remove(handmapname);
        }
    }

    public void RandAddMap()
    {
      var pos = GetNewMapPosition();
      var maps = _mapManager.Maps;
      Array<string> ablemaps = new Array<string>();
      foreach(var map in maps)
      {
        if(_mapManager.instancedMap.Contains(map)||(_player.CharacterData as PlayerData).Maps.Contains(map))
        {continue;}
        if(CheckSplice(this.id , map))
        {ablemaps.Add(map);}
      }
      if(ablemaps.Count <= 0)
      {return;}
      var id = (int)(GD.Randi() % ablemaps.Count);
      GD.Print("可用数",ablemaps.Count,"随机数",id);
      var randmapname = ablemaps[id];
      if(_mapManager.AddMap(randmapname ,pos,this.id,Owner.Name.ToString().ToLower()))
      {
        mapname = randmapname;
        (_player.CharacterData as PlayerData).MapinHand = "";
        (_player.CharacterData as PlayerData).Maps.Remove(randmapname);
        }
    }

    public void Dismap()
    {
      if(mapname == null||mapname == "")
      {return;}
      var pos = GetNewMapPosition();
      //GD.Print(mapname);
      if(_mapManager.RemoveMap(mapname))
      {
        (_player.CharacterData as PlayerData).Maps.Add(mapname);
      (_player.CharacterData as PlayerData).MapinHand = mapname;
      mapname = "";
      }
    }

    public Vector2 GetNewMapPosition()
    {
      int x = 224;
      int y = 112;
      int boolx = 0;
      int booly = 0;
      Map map = Owner as Map;
      switch(id)
      {
        case 1:
        boolx = 1;
        booly = -1;
        break;
        case 2:
        boolx = 1;
        booly = 1;
        break;
        case 3:
        boolx = -1;
        booly = 1;
        break;
        case 4:
        boolx = -1;
        booly = -1;
        break;
      }
      return new Vector2(x*boolx,y*booly) +map.Position;
    }

    public void ChangeMapName(string newname)
    {
      mapname = newname;
    }
  public override void OnInteractableBodyEntered(Node2D player)//信号，进入区域
  {
    if(mapname == "")
    {tips = "展开空间";}
    else{tips = "收起空间";}
      base.OnInteractableBodyEntered(player);
  }
  public override void OnInteractableBodyExited(Node2D player)
  {
    base.OnInteractableBodyExited(player);
  }

  public bool CheckSplice(int idFrom,string mapname)
  {
    bool isAdd = false;
    int idTo = 0;
		switch (idFrom)
		{
			case 1:
			idTo = 3;
			break;
			case 2:
			idTo = 4;
			break;
			case 3:
			idTo = 1;
			break;
			case 4:
			idTo = 2;
			break;
		}

    string basepath = "res://scences/";
		string path = basepath + mapname + ".tscn";
		var Game = GetNode<Game>("/root/Game");
		Map newmap= Game.InstantScene(path) as Map;
		var splices = newmap.GetChildren();
		foreach (var child in splices)
		{
			if(child is Splice)
			{
				Splice splice = child as Splice;
				if(splice.id == idTo)
				{
					isAdd = true;
				}
			}
		}
    //newmap.Dispose();
    return isAdd;
  }
}
